import scrapy
import json
from hot_barrage_spider.items import HotBarrageSpiderItem

class BarrageSpider(scrapy.Spider):
    name = 'barrage'
    # allowed_domains = ['https://api.bilibili.com/x/web-interface/popular?ps=50&pn=1']
    start_urls = ['https://api.bilibili.com/x/web-interface/popular?ps=50&pn=1']

    def parse(self, response):
        # print(response.text)
        texts = json.loads(response.text)
        lists = texts['data']['list']
        cnt = 1
        for list in lists:
            ranks = cnt
            bvid = list['bvid']
            cid = list['cid']
            pic = list['pic']
            title = list['title']
            owner = list['owner']['name']
            stat = list['stat']
            view = stat['view']
            danmaku = stat['danmaku']
            reply = stat['reply']
            favorite = stat['favorite']
            coin = stat['coin']
            share = stat['share']
            like = stat['like']
            short_link = list['short_link']
            cnt += 1
            barrages = HotBarrageSpiderItem(ranks=ranks,bvid=bvid,cid=cid,pic=pic,title=title,owner=owner,
                                            view=view,danmaku=danmaku,reply=reply,favorite=favorite,
                                            coin=coin,share=share,like=like,short_link=short_link)

            yield barrages

        # print(lists[0])


